<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 产品分类
 * @author LT
 */
class Classes extends Auth_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('goods/GoodsClassModel', '' , true);
	}
	
	/**
	 * 列表
	 */
	function index($index=1,$size=20){
//		$result = $this->GoodsClassModel->select($index,$size);
//		$data['list'] = $result['data'];
//		$this->load->library('Pagination');
//		$data['pages'] = $this->pagination->get_pages(base_url().index_page().'/manager/goodsclass/index',$result['total'],$size,$index);
//		$data['action'] = 'list';
//		$this->_view('manager/goods/class',$data);
		
		$result = $this->GoodsClassModel->selectAll();
		$view_data['list'] = $this->_build_tree($result);
		$view_data['action'] = 'list';
		$this->_view('manager/goods/class',$view_data);
	}

	/**
	 * 检测名称是否存在
	 * @param 名称 $name
	 */
	function isExistName($name) {
		$name = urldecode($name);
		$data['state'] = $this->GoodsClassModel->count(array('name'=>$name));
		$this->_output_json($data);
	}
	
	/**
	 * 新增或编辑显示父类
	 * @param $parentids 用“,”分隔的字符串.
	 */
	function _get_parent_names($parentids) {
		$parents = $this->GoodsClassModel->selectByIds($parentids);
		$parents_name = '';
		$count = count($parents);
		foreach ($parents as $parent) {
			$count--;
			$parents_name .= $parent['name'].($count == 0 ? '':' » ');
		}
		return $parents_name;
	}
	
	/**
	 * 产品分类选择
	 * @param $parentid 父类ID
	 */
	function choice($parentid=0) {
		$m = $this->input->get('m',true);//获取区域选择的方式，m=init 第一次请求
		if($m == 'init'){
			$parentname = '';
			$list = '';
			if($parentid == 0 ){
				$list = $this->GoodsClassModel->selectByParam(array('parentid'=>$parentid));
			}else{
				$area = $this->GoodsClassModel->selectById($parentid);
				if($area['parentid'] == 0){
					$parentname = $area['name'];
				}else{
					$parentname = $this->_get_parent_names($area['position']);
				}
				$list = $this->GoodsClassModel->selectByParam(array('parentid'=>0));
			}
			$data['from'] = $this->input->get('f',true);//来自哪里
			$data['parentname'] = $parentname;
			$data['list'] = $list;
			$this->_view('manager/goods/class_choice',$data);
		}else{//ajax请求，改变分类
			$list = $this->GoodsClassModel->selectByParam(array('parentid'=>$parentid));
			$data['list'] = $list;
			$this->_output_json($data);
		}
	}

	/**
	 * 新增
	 */
	function add($parentid=0) {
		$data = null;
		if($parentid != 0){
			$data['parentid'] = $parentid;
			$parent = $this->GoodsClassModel->selectById($parentid);
			if(count($parent)){
				if($parent['position'] != $parentid){
					$data['parentname'] = $this->_get_parent_names($parent['position']);
				}else{
					$data['parentname'] = $parent['name'];
				}
			}
		}
		$view_data['form'] = $this->_form('goodsclass','/manager/goods/classes/doadd', '/manager/goods/bean/fb_goodsclass.php',$data);
		$view_data['action'] = 'add';
		$this->_view('manager/goods/class',$view_data);
	}
	
	function doadd() {
		$class = $this->input->post('goodsclass',true);
		unset($class['id']);
		if(!$class['rebate']) $class['rebate'] = 0;
		if(!$class['sort']) $class['sort'] = 0;

		$data['state'] = $this->GoodsClassModel->insert($class);
		$data['message'] = $data['state'] > 0? '新增产品分类成功。':'新增产品分类失败！';
		$this->_output_json($data);
	}
	/**
	 * 编辑
	 */
	function edit($id=0) {
		$data['goodsclass'] = $this->GoodsClassModel->selectById($id);
		if(count($data['goodsclass'])){
			if($data['goodsclass']['parentid']){
				$data['goodsclass']['parentname'] = $this->_get_parent_names(str_replace(','.$id,'',$data['goodsclass']['position']));
			}
			$view_data['form'] = $this->_form('goodsclass','/manager/goods/classes/doedit', '/manager/goods/bean/fb_goodsclass.php',$data);
			$view_data['action'] = 'edit';
			$this->_view('manager/goods/class',$view_data);
		}else{
			$this->_view('error');
		}
	}
	function doedit() {
		$class = $this->input->post('goodsclass',true);
		if($class['parentid']){
			$parent = $this->GoodsClassModel->selectById($class['parentid']);
			$class['position'] = $parent['position'].','.$class['id'];
		}
		$data['state'] = $this->GoodsClassModel->update($class);
		$data['message'] = $data['state'] > 0 ? '编辑产品分类成功。':'编辑产品分类失败！';
		$this->_output_json($data);
	}
	function dodelete($id) {
		$data['state'] = $this->GoodsClassModel->delete($id);
		$data['message'] = $data['state'] > 0 ? '删除产品分类成功。':'删除产品分类失败！';
		$this->_output_json($data);
	}
}
?>
